/**
 * created by dqn 2018/3/7
 */

package main.controller;

import main.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;

@RestController
public class TestController {
    @Autowired
    MD5Util md5Util;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String upload(HttpSession httpSession) {
        String path = httpSession.getServletContext().getRealPath("/static/song/2cdc0f1e1df92f970717cfc2c5c14aaf.mp3");
        System.out.println(path);
        System.out.println(httpSession.getServletContext().getResourcePaths("/static/song/2cdc0f1e1df92f970717cfc2c5c14aaf.mp3"));
        File file = new File("/static/song/2cdc0f1e1df92f970717cfc2c5c14aaf.mp3");
        return path;
    }

//    /**
//     * 文件下载功能
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    @RequestMapping(value = "/down",method = RequestMethod.GET)
//    public void down(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") String file/*,@RequestParam double seek*/) throws Exception{
//        String filename = "D:\\data\\tmp\\"+file;
////        File readFile = new File(filename);
////
////        //获取输入流
////        InputStream bis = new BufferedInputStream(new FileInputStream(readFile));
////        // 进度条
//////        bis.skip((long) (bis.available()*seek));
////        //设置文件下载头
//        response.addHeader("Content-Disposition", "attachment;filename=" + file);
//        response.addHeader("Status",206+"");
//        response.addHeader("Accept-Ranges","bytes");
////        response.addHeader("Content-Range",request.getHeader("range"));
////        //1.设置文件Conte12ntType类型，这样设置，会自动判断下载文件类型
//        response.setContentType("audio/mpeg");
////        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
////        int len = 0;
////        byte[] buff = new byte[1024];
////        while (bis.read(buff) != -1){
////            out.write(buff);
////        }
//
////
////        while((len = bis.read()) != -1){
////            out.write(len);
////            out.flush();
////        }
////        out.close();
//        ServletOutputStream servletOutputStream = response.getOutputStream();
//        FileInputStream fileInputStream = new FileInputStream(new File(filename));
//        byte[] b = null;
//        while (fileInputStream.available() > 0){
//            if(fileInputStream.available() > 40960) {
//                b = new byte[40960];
//            }else {
//                b = new byte[fileInputStream.available()];
//            }
//            fileInputStream.read(b, 0, b.length);
//            servletOutputStream.write(b, 0, b.length);
//        }
//    }

}
